﻿using NFox.Pycad;
using System;
using System.Threading.Tasks;

namespace NFox.Pyeditor
{
    public class Application : PluginBase
    {

        public static Types.KeywordList KeywordList;

        protected override void OnLoading()
        {
            DynamicCompiler.AddSystemCommand("pye", new Action(ShowPyIde));
            Engine.AddSysProject(Engine.SupportPath.GetDirectory("pyedit"));
        }

        protected async override void OnLoaded()
        {
            await Task.Run(
                () =>
                {
                    //获取关键字列表
                    KeywordList = new Types.KeywordList();
                    Engine.TryReference("pyedit.dll");
                    dynamic pyesys = Engine.GetImportClass("pyedit.system");
                    foreach (var key in pyesys.statements)
                        KeywordList.AddStatement(key);
                    foreach (var kv in pyesys.builtins)
                        KeywordList.AddBuiltin(kv[0], kv[1]);
                    KeywordList.MakeHighlighting();
                });
        }

        Runtime.PyIdeWindow _win;

        public void ShowPyIde()
        {
            if (_win == null)
            {
                _win = new Runtime.PyIdeWindow();
                _win.Closed += (s, e) => _win = null;
            }
            _win.Show();
        }


    }
}
